<?php

require_once 'dompdf/dompdf_config.inc.php';

class PdfController extends Zend_Controller_Action
{
    protected $db;
    
    public function init()
    {
        /* Initialize action controller here */
        /*if (!Zend_Auth::getInstance()->hasIdentity()) {
            $param = array(
            'action' => 'admin',
            'controller' => 'index',
            'msg' => 'Session Timeout silahkan login kembali !'
            );
            $this->_redirect($this->view->url($param, 'default', true));
        }
         * 
         */

        $session = new Zend_Session_Namespace('Zend_Auth');
        $this->sess = $session->data;

        $sessionAdmin = new Zend_Session_Namespace('Zend_Auth_Admin');
        $this->sessAdmin = $sessionAdmin->data;

        if(!isset($this->sess) && !isset($this->sessAdmin)){
            echo "TIMEOUT";
            die();
        }

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $this->request = $this->getRequest();
        $this->param = $this->request->getParams();

    }

    

    public function indexAction()
    {
        //
    }

    

    public function mutasiAction()
    {

        set_time_limit( 0 );

        $transType = $this->param['transType'];
        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');


        $memberId = $this->sess->member_id;
        $memberCode = $this->sess->member_code;
        
        $modelRek = new Application_Model_Rekening();
        $data = $modelRek->getTotalSaldo($memberId);

        $modelFunc = new Application_Model_Functions();
        $today =  $modelFunc->gatDayIndoName(date('l')).', '.date('d F Y H:i:s');

        $tmp = '<style type="text/css">
                table.dotBorder { border-collapse:collapse; }
                table.dotBorder td, table.dotBorder th { border:1px dotted black;padding:5px;}

                table.myTable { border-collapse:collapse; }
                table.myTable td, table.myTable th { border:1px solid black;padding:5px; }
                </style>';
        $tmp .= '<h3><center>Mutasi Rekening</center></h3>';
        $tmp .= '<table cellpadding="5px">
                    <tr>
                        <td width="100">Nomor Akun</td>
                        <td>: '.$data['account_num'].'</td>
                    </tr>
                    <tr>
                        <td>Nama Akun</td>
                        <td>: '.$data['account_name'].'</td>
                    </tr>
                    <tr>
                        <td>Periode</td>
                        <td>: '.$this->param['date1'].' s/d '.$this->param['date2'].'</span></td>
                    </tr>
                    <tr>
                        <td>Waktu Cetak</td>
                        <td>: '.$today.'</span></td>
                    </tr>
            </table><br><br>';

       $tmp .= '<table id="vm" class="myTable">
            <thead>
                    <tr>
                            <th>No</th>
                            <th>Tgl Transaksi</th>
                            <th>Jenis Transaksi</th>
                            <th align="right">Kredit</th>
                            <th align="right">Debet</th>
                    </tr>
            </thead>
            <tbody>';

        $dataRow = $modelRek->getMutasi($memberCode, $transType, $date1, $date2, "", "");
        $total = $dataRow[0];
        
        $debit = 0;
        $kredit = 0;
        $no = 0;
        foreach($dataRow[2] as $dt){
            if ($dt["debkred"] == 'K' ){
                $kredit = $kredit + $dt["amt1"];
            }else{
                $debit = $debit + $dt["amt1"];
            }

            $no++;

            $tmp .= '<tr>
                        <td>'.$no.'</td>
                        <td>'.$dt["trans_dtm"].'</td>
                        <td>'.$dt["descr"].'</td>
                        <td align="right">'.$dt["kredit"].'</td>
                        <td align="right">'.$dt["debit"].'</td>
                     </tr>';

        }


        $debit = number_format($debit, 2);
        $kredit = number_format($kredit, 2);

        $tmp .= '<tr>
                        <td></td>
                        <td></td>
                        <td><b>Total</b></td>
                        <td align="right"><b>'.$kredit.'</b></td>
                        <td align="right"><b>'.$debit.'</b></td>
                     </tr>';

        //$tmp['footer'][] = array('amt1' => $kredit, 'acc_num1' => 'Average');
        //$tmp['footer'][] = array('debit' => $debit, 'kredit' => $kredit, 'descr' => 'Sub Total');

        $tmp .= '</tbody>
            </table><br><br>';

        $tmp .= '<table cellpadding="5px">
                    <tr>
                        <td width="100">Total Kredit</td>
                        <td>:</td>
                        <td align="right">'.number_format($data['kredit'], 2).'</td>
                    </tr>
                    <tr>
                        <td>Total Debet</td>
                        <td>:</td>
                        <td align="right">'.number_format($data['debet'], 2).'</td>
                    </tr>                    
                    <tr>
                        <td>Saldo Akhir</td>
                        <td>:</td>
                        <td align="right">'.$data['balance'].'</td>
                    </tr>
            </table>';

        //print_r($data);
        //die();
        $file = "mutasi-".$memberCode."-". date( "YmdHis" ) . ".pdf";
        $filename = APPLICATION_PATH . "/../public/pdf/".$file;

        /*Create PDF File
         *
         */
        $dompdf = new DOMPDF();
        $dompdf->load_html($tmp);
        $dompdf->render();

        //Kalau ini filenya langsung didownload
        //$dompdf->stream('public/pdf/'.date('dmYHis').'_order.pdf');

        //Kalau ini filenya disave di server
        $pdf = $dompdf->output();
        file_put_contents($filename, $pdf);


        echo $file;
        

    }


    public function mutasiadminAction()
    {

        set_time_limit( 0 );

        $transType = $this->param['transType'];
        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');


        
        $memberCode = $this->param['noAkun'];

        $modelReport = new Application_Model_Report();
        $data = $modelReport->getTotalSaldo();

        $modelFunc = new Application_Model_Functions();
        $today =  $modelFunc->gatDayIndoName(date('l')).', '.date('d F Y H:i:s');

        $tmp = '<style type="text/css">
                table.dotBorder { border-collapse:collapse; }
                table.dotBorder td, table.dotBorder th { border:1px dotted black;padding:5px;}

                table.myTable { border-collapse:collapse; }
                table.myTable td, table.myTable th { border:1px solid black;padding:5px; }
                </style>';
        $tmp .= '<h3><center>Mutasi Rekening</center></h3>';
        $tmp .= '<table cellpadding="5px">
                    <tr>
                        <td width="100">Nomor Akun</td>
                        <td>: '.$data['account_num'].'</td>
                    </tr>
                    <tr>
                        <td>Nama Akun</td>
                        <td>: '.$data['account_name'].'</td>
                    </tr>
                    <tr>
                        <td>Periode</td>
                        <td>: '.$this->param['date1'].' s/d '.$this->param['date2'].'</span></td>
                    </tr>
                    <tr>
                        <td>Waktu Cetak</td>
                        <td>: '.$today.'</span></td>
                    </tr>
            </table><br><br>';

       $tmp .= '<table id="vm" class="myTable">
            <thead>
                    <tr>
                            <th>No</th>
                            <th>Tgl Transaksi</th>
                            <th>Jenis Transaksi</th>
                            <th align="right">Kredit</th>
                            <th align="right">Debet</th>
                    </tr>
            </thead>
            <tbody>';

        $modelRek = new Application_Model_Rekening();
        $dataRow = $modelRek->getMutasi($memberCode, $transType, $date1, $date2, "", "");
        $total = $dataRow[0];

        $debit = 0;
        $kredit = 0;
        $no = 0;
        foreach($dataRow[2] as $dt){
            if ($dt["debkred"] == 'K' ){
                $kredit = $kredit + $dt["amt1"];
            }else{
                $debit = $debit + $dt["amt1"];
            }

            $no++;

            $tmp .= '<tr>
                        <td>'.$no.'</td>
                        <td>'.$dt["trans_dtm"].'</td>
                        <td>'.$dt["descr"].'</td>
                        <td align="right">'.$dt["kredit"].'</td>
                        <td align="right">'.$dt["debit"].'</td>
                     </tr>';

        }


        $debit = number_format($debit, 2);
        $kredit = number_format($kredit, 2);

        $tmp .= '<tr>
                        <td></td>
                        <td></td>
                        <td><b>Total</b></td>
                        <td align="right"><b>'.$kredit.'</b></td>
                        <td align="right"><b>'.$debit.'</b></td>
                     </tr>';

        //$tmp['footer'][] = array('amt1' => $kredit, 'acc_num1' => 'Average');
        //$tmp['footer'][] = array('debit' => $debit, 'kredit' => $kredit, 'descr' => 'Sub Total');

        $tmp .= '</tbody>
            </table><br><br>';

        $tmp .= '<table cellpadding="5px">
                    <tr>
                        <td width="100">Total Kredit</td>
                        <td>:</td>
                        <td align="right">'.number_format($data['kredit'], 2).'</td>
                    </tr>
                    <tr>
                        <td>Total Debet</td>
                        <td>:</td>
                        <td align="right">'.number_format($data['debet'], 2).'</td>
                    </tr>
                    <tr>
                        <td>Saldo Akhir</td>
                        <td>:</td>
                        <td align="right">'.$data['balance'].'</td>
                    </tr>
            </table>';

        //print_r($data);
        //die();
        $file = "mutasi-".$memberCode."-". date( "YmdHis" ) . ".pdf";
        $filename = APPLICATION_PATH . "/../public/pdf/".$file;

        /*Create PDF File
         *
         */
        $dompdf = new DOMPDF();
        $dompdf->load_html($tmp);
        $dompdf->render();

        //Kalau ini filenya langsung didownload
        //$dompdf->stream('public/pdf/'.date('dmYHis').'_order.pdf');

        //Kalau ini filenya disave di server
        $pdf = $dompdf->output();
        file_put_contents($filename, $pdf);


        echo $file;


    }


    public function bonustrxAction()
    {

        set_time_limit( 0 );

        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');

        $memberId = $this->sess->member_id;
        $accNum = $this->sess->member_code;

        $modelRek = new Application_Model_Rekening();
        $data = $modelRek->getTotalSaldo($memberId);

        $modelFunc = new Application_Model_Functions();
        $today =  $modelFunc->gatDayIndoName(date('l')).', '.date('d F Y H:i:s');

        $tmp = '<style type="text/css">
                table.dotBorder { border-collapse:collapse; }
                table.dotBorder td, table.dotBorder th { border:1px dotted black;padding:5px;}

                table.myTable { border-collapse:collapse; }
                table.myTable td, table.myTable th { border:1px solid black;padding:5px; }
                </style>';
        $tmp .= '<h3><center>Bonus Transaksi</center></h3>';
        $tmp .= '<table cellpadding="5px">
                    <tr>
                        <td width="100">Nomor Akun</td>
                        <td>: '.$data['account_num'].'</td>
                    </tr>
                    <tr>
                        <td>Nama Akun</td>
                        <td>: '.$data['account_name'].'</td>
                    </tr>
                    <tr>
                        <td>Periode</td>
                        <td>: '.$this->param['date1'].' s/d '.$this->param['date2'].'</span></td>
                    </tr>
                    <tr>
                        <td>Waktu Cetak</td>
                        <td>: '.$today.'</span></td>
                    </tr>
            </table><br><br>';

       $tmp .= '<table id="vm" class="myTable">
            <thead>
                    <tr>
                            <th>No</th>
                            <th>Tanggan</th>
                            <th align="right">Bonus</th>
                            <th>Keterangan</th>
                    </tr>
            </thead>
            <tbody>';

        $dataRow = $modelRek->getBonusTransaction($accNum, $date1, $date2, "", "");
        $total = $dataRow[0];

        $total = 0;
        $no = 0;
        foreach($dataRow[1] as $dt){
            
            $total = $total + $dt["amount"];
            
            $no++;

            $tmp .= '<tr>
                        <td>'.$no.'</td>
                        <td>'.$dt["trx_dtm"].'</td>
                        <td align="right">'.$dt["amount_rp"].'</td>
                        <td>'.$dt["notes_txt"].'</td>
                     </tr>';

        }


        $tmp .= '<tr>
                        <td></td>
                        <td><b>Total</b></td>
                        <td align="right"><b>'.number_format($total, 2).'</b></td>
                        <td></td>
                     </tr>';

        $tmp .= '</tbody>
            </table><br><br>';

        //print_r($data);
        //die();
        $file = "bonustrx-".$accNum."-". date( "YmdHis" ) . ".pdf";
        $filename = APPLICATION_PATH . "/../public/pdf/".$file;

        /*Create PDF File
         *
         */
        $dompdf = new DOMPDF();
        $dompdf->load_html($tmp);
        $dompdf->render();

        //Kalau ini filenya langsung didownload
        //$dompdf->stream('public/pdf/'.date('dmYHis').'_order.pdf');

        //Kalau ini filenya disave di server
        $pdf = $dompdf->output();
        file_put_contents($filename, $pdf);


        echo $file;


    }


    public function trackingAction()
    {

        set_time_limit( 0 );

        $transType = $this->param['transType'];
        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');


        $memberId = $this->sess->member_id;
        $memberCode = $this->sess->member_code;

        $modelPurchase = new Application_Model_Purchase();
        
        $modelFunc = new Application_Model_Functions();
        $today =  $modelFunc->gatDayIndoName(date('l')).', '.date('d F Y H:i:s');

        $tmp = '<style type="text/css">
                table.dotBorder { border-collapse:collapse; }
                table.dotBorder td, table.dotBorder th { border:1px dotted black;padding:5px;}

                table.myTable { border-collapse:collapse; }
                table.myTable td, table.myTable th { border:1px solid black;padding:5px; }
                </style>';
        $tmp .= '<h3><center>Tracking Transaction</center></h3>';
        $tmp .= '<table cellpadding="5px">
                    
                    <tr>
                        <td>Periode</td>
                        <td>: '.$this->param['date1'].' s/d '.$this->param['date2'].'</span></td>
                    </tr>
                    <tr>
                        <td>Waktu Cetak</td>
                        <td>: '.$today.'</span></td>
                    </tr>
            </table><br><br>';

       $tmp .= '<table id="vm" class="myTable">
            <thead>
                    <tr>
                            <th>No</th>
                            <th>Tgl Transaksi</th>
                            <th>Jenis Transaksi</th>
                            <th>Telp/Seluler/PLN</th>
                            <th>Status</th>
                    </tr>
            </thead>
            <tbody>';

        $dataRow = $modelPurchase->getTrackingTransaction($memberCode, $transType, $date1, $date2, "", "");
        $total = $dataRow[0];

        $debit = 0;
        $kredit = 0;
        $no = 0;
        foreach($dataRow[2] as $dt){
            if ($dt["debkred"] == 'K' ){
                $kredit = $kredit + $dt["amt1"];
            }else{
                $debit = $debit + $dt["amt1"];
            }

            $no++;

            $tmp .= '<tr>
                        <td>'.$no.'</td>
                        <td>'.$dt["trans_dtm"].'</td>
                        <td>'.$dt["descr"].'</td>
                        <td>'.$dt["external_param"].'</td>
                        <td>'.$dt["external_resp"].'</td>
                     </tr>';

        }


        $debit = number_format($debit, 2);
        $kredit = number_format($kredit, 2);

        $tmp .= '<tr>
                        <td></td>
                        <td></td>
                        <td><b>Total</b></td>
                        <td align="right"><b>'.$kredit.'</b></td>
                        <td align="right"><b>'.$debit.'</b></td>
                     </tr>';

        //$tmp['footer'][] = array('amt1' => $kredit, 'acc_num1' => 'Average');
        //$tmp['footer'][] = array('debit' => $debit, 'kredit' => $kredit, 'descr' => 'Sub Total');

        $tmp .= '</tbody>
            </table><br><br>';

       

        //print_r($data);
        //die();
        $file = "tracking-".$memberCode."-". date( "YmdHis" ) . ".pdf";
        $filename = APPLICATION_PATH . "/../public/pdf/".$file;

        /*Create PDF File
         *
         */
        $dompdf = new DOMPDF();
        $dompdf->load_html($tmp);
        $dompdf->render();

        //Kalau ini filenya langsung didownload
        //$dompdf->stream('public/pdf/'.date('dmYHis').'_order.pdf');

        //Kalau ini filenya disave di server
        $pdf = $dompdf->output();
        file_put_contents($filename, $pdf);


        echo $file;


    }

    public function trackingadminAction()
    {

        set_time_limit( 0 );

        $transType = $this->param['transType'];
        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');

        $memberCode = $this->param['noAkun'];

        
        $modelFunc = new Application_Model_Functions();
        $today =  $modelFunc->gatDayIndoName(date('l')).', '.date('d F Y H:i:s');

        $tmp = '<style type="text/css">
                table.dotBorder { border-collapse:collapse; }
                table.dotBorder td, table.dotBorder th { border:1px dotted black;padding:5px;}

                table.myTable { border-collapse:collapse; }
                table.myTable td, table.myTable th { border:1px solid black;padding:5px; }
                </style>';
        $tmp .= '<h3><center>Tracking Transaksi</center></h3>';
        $tmp .= '<table cellpadding="5px">

                    <tr>
                        <td>Periode</td>
                        <td>: '.$this->param['date1'].' s/d '.$this->param['date2'].'</span></td>
                    </tr>
                    <tr>
                        <td>Waktu Cetak</td>
                        <td>: '.$today.'</span></td>
                    </tr>
            </table><br><br>';

       $tmp .= '<table id="vm" class="myTable">
            <thead>
                    <tr>
                            <th>No</th>
                            <th>Tgl Transaksi</th>
                            <th>Nomor Akun</th>
                            <th>Nama Akun</th>
                            <th>Jenis Transaksi</th>
                            <th>Telp/Seluler/PLN</th>
                            <th>Status</th>
                    </tr>
            </thead>
            <tbody>';

        $modelReport = new Application_Model_Report();
        $dataRow = $modelReport->getTrackingTransaction($memberCode, $transType, $date1, $date2, "", "");
        $total = $dataRow[0];

        $debit = 0;
        $kredit = 0;
        $no = 0;
        foreach($dataRow[2] as $dt){


            $no++;

            $tmp .= '<tr>
                        <td>'.$no.'</td>
                        <td>'.$dt["trans_dtm"].'</td>
                        <td>'.$dt["account_num"].'</td>
                        <td>'.$dt["account_name"].'</td>
                        <td>'.$dt["descr"].'</td>
                        <td>'.$dt["external_param"].'</td>
                        <td>'.$dt["external_resp"].'</td>
                     </tr>';

        }


        $tmp .= '</tbody>
            </table><br><br>';



        //print_r($data);
        //die();
        $file = "tracking-".$memberCode."-". date( "YmdHis" ) . ".pdf";
        $filename = APPLICATION_PATH . "/../public/pdf/".$file;

        /*Create PDF File
         *
         */
        $dompdf = new DOMPDF();
        $dompdf->load_html($tmp);
        $dompdf->render();

        //Kalau ini filenya langsung didownload
        //$dompdf->stream('public/pdf/'.date('dmYHis').'_order.pdf');

        //Kalau ini filenya disave di server
        $pdf = $dompdf->output();
        file_put_contents($filename, $pdf);


        echo $file;


    }
}

